package com.chuanyang.driverh.mainmy.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainmy.adapter.CarOwnerCommentsdapter;
import com.chuanyang.driverh.responseresult.CommentData;
import com.chuanyang.driverh.responseresult.CommentResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class CommentFragment extends BaseListFragment {
    private String fleetId;
    private String driverId;
    private boolean isTeamComment;

    public static CommentFragment newInstance(String driverId, String fleetId) {
        CommentFragment fragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("driverId", driverId);
        bundle.putString("fleetId", fleetId);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            fleetId = bundle.getString("fleetId");
            driverId = bundle.getString("driverId");
        }
    }

    @Override
    public BaseListAdapter createAdapter() {
        ArrayList<CommentData.DataBean> commentlist = new ArrayList();
        return new CarOwnerCommentsdapter(getActivity(), commentlist);
    }

    private void getComment(boolean isTeamComment) {
        DialogUtil.showLoadingDialog(getActivity(),"评论...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        if (isTeamComment) {
            params.put("fleetId", fleetId);
        } else {
            params.put("driverId", driverId);

        }
        MyHttpUtil.getByDriverOrFleet(getActivity(),params, new MyCallback<CommentResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CommentResult result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    if (result.getData().getData() != null)
                    {
                        setDataResult(result.getData().getData());
                        if(result.getData().getData().size()==0)
                        {
                            onShowNull(R.mipmap.nodata);
                            setTextNull("当前暂无评论！");
                        }
                    }
                }
            }
        });


    }

    @Override
    public void initView(View view) {
        super.initView(view);
        if (!TextUtils.isEmpty(driverId)) {
            isTeamComment = false;
        } else if (!TextUtils.isEmpty(fleetId)) {
            isTeamComment = true;
        }
        mCurrentPage = 1;
        getComment(isTeamComment);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getComment(isTeamComment);
    }

    @Override
    public void onGetMore() {
        getComment(isTeamComment);
    }

    @Override
    protected void initListener() {

    }
}
